<?php
libxml_disable_entity_loader(false);
session_start();
$langfile = $_SERVER["DOCUMENT_ROOT"].'/lang/default.xml';
switch ($_SERVER['HTTP_HOST']){
	case "discovermapsforbusiness.tw":
		$_SESSION['lang'] = "zh-tw";
		break;
	case "discovermapsforbusiness.jp":
		$_SESSION['lang'] = "ja-jp";
		break;
	case "discovermapsforbusiness.kr":
		$_SESSION['lang'] = "euc-kr";
		break;
	default:
		$_SESSION['lang'] = null;
		break;
}

if (isset($_SESSION['lang'])) {
	switch ($_SESSION['lang']) {
		case "zh-tw" :
			$langfile = $_SERVER["DOCUMENT_ROOT"].'/lang/zh-tw.xml';
			break;
		case "ja-jp" :
			$langfile = $_SERVER["DOCUMENT_ROOT"].'/lang/ja-jp.xml';
			break;
		case "euc-kr":
			$langfile = $_SERVER["DOCUMENT_ROOT"].'/lang/euc-kr.xml';
			break;
	}
}

if (file_exists($langfile )) {
	$lang = simplexml_load_file($langfile);
} else {
	exit('please create a language file for this domain.');
}
function full_url()
{
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port . $_SERVER['REQUEST_URI'];
}

function hostname() 
{
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = substr(strtolower($_SERVER["SERVER_PROTOCOL"]), 0, strpos(strtolower($_SERVER["SERVER_PROTOCOL"]), "/")) . $s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	return $protocol . "://" . $_SERVER['SERVER_NAME'] . $port;
}

function getBrowser()
{
	$u_agent = $_SERVER['HTTP_USER_AGENT'];
	$ub = '';
	if(preg_match('/MSIE/i',$u_agent))
	{
		$ub = "IE";
	}
	elseif(preg_match('/Firefox/i',$u_agent))
	{
		$ub = "Mozilla";
	}
	elseif(preg_match('/Safari/i',$u_agent))
	{
		$ub = "Safari";
	}
	elseif(preg_match('/Chrome/i',$u_agent))
	{
		$ub = "Chrome";
	}
	elseif(preg_match('/Opera/i',$u_agent))
	{
		$ub = "Opera";
	}
	return $ub;
}

function replaceIndustryFirms($copy) {
	$replacement = "";
	if (isset($_GET['industry'])) {
		$rep_industry = $_GET['industry'];
		
		switch ($rep_industry) {
			case "telcoinfo":
				$replacement = "Telecommunications firms";
				break;
			case "financialinfo":
				$replacement = "Financial Services firms";
				break;
			case "resourceinfo":
				$replacement = "Mining and Oil &amp; Gas companies";
				break;
			case "governmentinfo":
				$replacement = "State and Local Governments";
				break;
			case "utilitiesinfo":
				$replacement = "Utilities";
				break;	
		}
		$copy = str_replace("INDUSTRYFIRMS", $replacement, $copy);
	}	
	return $copy;
}

function buildLanguageSelect() {
	$languages = array(
		"English" =>  "ENGLISH",
		"Chinese" =>  "中文", 
		"Japanese" => "日本語",
		"Korean" => "한국어"
	);
	$siteLinks = array( // change the links to production URLs once live
		"English" => "http://discovermapsforbusiness.com",
		"Chinese" => "http://discovermapsforbusiness.tw",
		"Japanese" => "http://discovermapsforbusiness.jp",
        "Korean" => "http://discovermapsforbusiness.kr"
	); 

	$currentLanguage = $languages["English"];

	switch ($_SESSION['lang']) {
		
        case "zh-tw" :
			$currentLanguage = $languages["Chinese"];
            
            printf('<div id="languageSelect"><span class="language">%s</span></div>', $currentLanguage);
	        printf('<div class="dropdown">');
            printf('<a href="%s">%s</a>', $siteLinks["English"], $languages["English"]);
            printf('<a href="%s">%s</a>', $siteLinks["Japanese"], $languages["Japanese"]);
            printf('<a href="%s">%s</a>', $siteLinks["Korean"], $languages["Korean"]);
            printf('</div>');
			break;
        
		case "ja-jp" :
			$currentLanguage = $languages["Japanese"];
            
            printf('<div id="languageSelect"><span class="language">%s</span></div>', $currentLanguage);
	        printf('<div class="dropdown">');
            printf('<a href="%s">%s</a>', $siteLinks["Chinese"], $languages["Chinese"]);
            printf('<a href="%s">%s</a>', $siteLinks["English"], $languages["English"]);
            printf('<a href="%s">%s</a>', $siteLinks["Korean"], $languages["Korean"]);
            printf('</div>');
			break;
        case "euc-kr" :
            $currentLanguage = $languages["Korean"];
            
            printf('<div id="languageSelect"><span class="language">%s</span></div>', $currentLanguage);
	        printf('<div class="dropdown">');
            printf('<a href="%s">%s</a>', $siteLinks["Chinese"], $languages["Chinese"]);
            printf('<a href="%s">%s</a>', $siteLinks["English"], $languages["English"]);
            printf('<a href="%s">%s</a>', $siteLinks["Japanese"], $languages["Japanese"]);
            printf('</div>');
			break;
        default : 
            $currentLanguage = $languages["English"];
            
            printf('<div id="languageSelect"><span class="language">%s</span></div>', $currentLanguage);
	        printf('<div class="dropdown">');
            printf('<a href="%s">%s</a>', $siteLinks["Chinese"], $languages["Chinese"]);
            printf('<a href="%s">%s</a>', $siteLinks["Japanese"], $languages["Japanese"]);
            printf('<a href="%s">%s</a>', $siteLinks["Korean"], $languages["Korean"]);
            printf('</div>');
            break;
	}
}

?>
